package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.User;
import service.UserService;


@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	
	private UserService userService = new UserService();
	
	private static final long serialVersionUID = 1L;
       
   
    public LoginServlet() {
        super();
        
    }

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
		
}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		
		User user = new User();
		user.setUserName(userName);
		user.setPassword(password);
		
		if(userService.validateUser(user)) {
			
			request.setAttribute("userName", userName);
			request.getRequestDispatcher("hello.jsp").forward(request, response);
		}else {
			
			request.setAttribute("errorMsg", "用户命 or 密码错误");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
			response.getWriter().append("Served at: ").append(request.getContextPath());
		}

		
		
		
		
		
	

}
